<template>
  <el-dialog
    :visible.sync="visible"
    :title="!dataForm.id ? $t('add') : $t('update')"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    @close="CloseDialog"
  >
    <el-form
      :model="dataForm"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmitHandle()"
      :label-width="$i18n.locale === 'en-US' ? '120px' : '100px'"
    >
      <el-form-item label="房源" prop="houseIdList">
        <el-input v-model="dataForm.houseIdList.join()" placeholder="房源" @focus="houseFocus"></el-input>
      </el-form-item>
      <el-form-item label="陪看人部门">
        <el-cascader
          style="width:100%"
          v-model="deptSelectVal"
          :options="this.$store.state.DeptList"
          :show-all-levels="false"
          @change="deptIdChange"
          :props="deptProps"
          ref="myCascader"
          clearable
        ></el-cascader>
      </el-form-item>
      <el-form-item label="陪看人">
        <el-select
          v-model="dataForm.userpId"
          placeholder="请选择"
          style="width:100%"
          clearable
          :disabled="dataForm.deptId==''"
        >
          <el-option
            v-for="item in maintainList"
            :key="item.id"
            :label="item.realName"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="陪看人部门">
        <ren-dept-tree v-model="dataForm.deptpId" :placeholder="$t('dept.title')"></ren-dept-tree>
      </el-form-item>

      <el-form-item label="陪看人" prop="userpId">
        <el-select v-model="dataForm.userpId" style="width:100%" placeholder="陪看人">
          <el-option
            v-for="item in UserList"
            :key="item.id"
            :label="item.realName"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>-->
      <el-form-item label="带看反馈" prop="evaluate">
        <el-input v-model="dataForm.evaluate" placeholder="带看反馈" type="textarea" :rows="2"></el-input>
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
    <Housing @setHousing="setHousingFun" ref="HousingRef"></Housing>
  </el-dialog>
</template>

<script>
import debounce from "lodash/debounce";
import Housing from "../../Housing/Housing";
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: "",
        customerId: this.$route.query.ID ? this.$route.query.ID : "",
        customerType: this.$route.query.customerType
          ? this.$route.query.customerType
          : "",
        houseIdList: [],
        evaluate: "",
        userpId: "",
        deptpId: ""
      },
      options: [],
      loading: false,
      UserList: [],
      deptSelectVal: "",
      deptProps: {
        value: "id",
        label: "name",
        checkStrictly: true
      },
      maintainList: this.$store.state.maintainList
    };
  },
  methods: {
    init() {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.getInfo();
        }
      });
    },
    deptIdChange(val) {
      let a = "";
      if (this.$refs.myCascader.getCheckedNodes()[0]) {
        a = this.$refs.myCascader.getCheckedNodes()[0].value;
        this.dataForm.deptpId = a;
        this.getUserList(a);
      } else {
        this.dataForm.deptpId = "";
      }
    },
    // 获取部门员工列表
    getUserList(deptId) {
      let _this = this;
      this.$http
        .get(`/sys/user/deptUserList`, {
          params: {
            deptId: deptId
          }
        })
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          _this.maintainList = res.data;
        })
        .catch(() => {});
    },
    // 获取信息
    getInfo() {
      this.$http
        .get(`/customer/customervisit/${this.dataForm.id}`)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.dataForm = {
            ...this.dataForm,
            ...res.data
          };
        })
        .catch(() => {});
    },
    // 获取部门员工列表
    getDeptUser() {
      this.$http
        .get(`/sys/user/deptUserList`, {
          params: {
            deptId: this.dataForm.deptpId
          }
        })
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.UserList = res.data;
        })
        .catch(() => {});
    },
    // 表单提交
    dataFormSubmitHandle: debounce(
      function() {
        this.$refs["dataForm"].validate(valid => {
          if (!valid) {
            return false;
          }
          this.$http[!this.dataForm.id ? "post" : "put"](
            "/customer/customervisit/",
            this.dataForm
          )
            .then(({ data: res }) => {
              if (res.code !== 0) {
                return this.$message.error(res.msg);
              }
              this.$message({
                message: this.$t("prompt.success"),
                type: "success",
                duration: 500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            })
            .catch(() => {});
        });
      },
      1000,
      { leading: true, trailing: false }
    ),
    setHousingFun(obj) {
      this.dataForm.houseIdList.push(obj.houseId);
    },
    houseFocus() {
      // this.ifHousing=true;
      this.$refs.HousingRef.info();
    },
    CloseDialog() {
      this.$emit("CloseDialog");
    }
  },

  components: {
    Housing
  },
  watch: {
    "dataForm.deptpId": {
      handler(nowVal, oldVal) {
        this.getDeptUser();
      }
    }
  }
};
</script>
